package com.jic.market.controller.manage;
/**
 * 分摊管理表 前端控制器
 *
 * @author xuyongliang
 * @since 2020-03-15
 */

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.entity.AllocationModel;
import com.jic.market.request.manager.allocation.*;
import com.jic.market.response.manager.allocation.AllocationQueryResponse;
import com.jic.market.service.AllocationService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.service.ResponseMessage;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/allocation")
public class AllocationController {

    @Autowired
    private AllocationService modelService;

    /**
      * 分摊管理表 新增
      * @param req
      * @return
      */
    @PostMapping(value = "/add")
    @ResponseBody
    public RestResult allocationAdd(@RequestBody AllocationAddRequest req) {
        AllocationModel allocationModel = new AllocationModel();
        BeanUtils.copyProperties(req, allocationModel);
        this.modelService.allocationAdd(allocationModel);
        return RestResult.success("ok");
    }

    /**
     * 分摊管理表 编辑
     * @param req
     * @return
     */
    @PostMapping(value = "/edit")
    @ResponseBody
    public RestResult allocationEdit(@RequestBody AllocationEditRequest req) {
        AllocationModel modifyEntity = new AllocationModel();
        AllocationModel whereCondition =new AllocationModel();
        this.modelService.allocationEdit(modifyEntity,whereCondition);
        return RestResult.success("ok");
    }


    /**
     * 分摊管理表 删除
     * @param req
     * @return
     */
    @PostMapping(value = "/delete")
    @ResponseBody
    public RestResult allocationDelete(@RequestBody AllocationDeleteRequest req) {
        this.modelService.allocationDelete(req.getId());
        return RestResult.success("ok");
    }

    /**
     * 分摊管理表 查询详情
     * @param req
     * @return
     */
    @PostMapping(value = "/query")
    @ResponseBody
    public RestResult<AllocationQueryResponse> allocationQuery(@RequestBody AllocationQueryRequset req) {
        AllocationQueryResponse innerResponse =new AllocationQueryResponse();

        AllocationModel modelEntity =new AllocationModel();
        BeanUtils.copyProperties(req, modelEntity);
        AllocationModel model = this.modelService.allocationQuery(modelEntity);
        if(model !=null){
            BeanUtils.copyProperties(model, innerResponse);
        }
        return RestResult.success(innerResponse);
    }


    /**
     * 分摊管理表 分页查询
     * @param req
     * @return
     */
    @PostMapping(value = "/queryPageList")
    @ResponseBody
    public RestResult<PageResult<AllocationQueryResponse>> allocationQueryPageList(
            @RequestBody AllocationQueryPageListRequest req) {
        log.info("分页查询分摊信息，入参{}", JSON.toJSONString(req));
        Page pageQuery =new Page();
        pageQuery.setStart(req.getStart());
        pageQuery.setLimit(req.getLimit());

//        if(req.getPromotionId()==null && req.getPromName()==null && req.getPromType()==null
//                && req.getStartTime()==null && req.getEndTime() ==null){
//            req.setStartTime(DateUtils.addDays(new Date(), -7));
//            req.setEndTime(new Date());
//        }
        AllocationModel am =new AllocationModel();
        BeanUtils.copyProperties(req, am);
        PageResult<AllocationQueryResponse> pageResult = (PageResult<AllocationQueryResponse>) modelService.allocationQueryPageList(am, pageQuery);
        log.info("分页查询分摊信息，出参{}", JSON.toJSONString(pageResult));
        return RestResult.success(pageResult);
    }

}
